package com.wangtianxiang.sxp.common.exception;

import com.wangtianxiang.sxp.common.http.FeedBackBody;
import com.wangtianxiang.sxp.common.http.ResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 * @description  
 * @author wangtianxiang
 * @date: 2021/6/29 18:14
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    private FeedBackBody<?> Exception(HttpServletRequest request, Exception e) {
        logger.error("method:{},requestUri:{},error:{}",request.getMethod(),request.getRequestURI(),e);
        return ResponseEntity.setForDataError(null).setDetail("系统内部错误");
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    private FeedBackBody<?> MethodArgumentNotValidException(MethodArgumentNotValidException e) {

        return ResponseEntity.setForDataError(null).setDetail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());

    }

    @ExceptionHandler({AppException.class,ServletException.class})
    private FeedBackBody<?> AppException(Exception e) {
        return ResponseEntity.setForDataError(null).setDetail(e.getMessage());
    }
}
